package com.zhejiangzhengyuan.municipal_flooded_road.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhejiangzhengyuan.municipal_common.entity.DeviceMonitorValue;
import com.zhejiangzhengyuan.municipal_common.entity.ElectronicData;
import com.zhejiangzhengyuan.municipal_common.entity.vo.DeviceMonitorValueVo;
import com.zhejiangzhengyuan.municipal_common.entity.vo.DeviceVo;
import com.zhejiangzhengyuan.municipal_common.mapper.ElectronicDataMapper;
import com.zhejiangzhengyuan.municipal_common.response.ResponseVo;
import com.zhejiangzhengyuan.municipal_common.service.DeviceMonitorValueService;
import com.zhejiangzhengyuan.municipal_common.service.DeviceService;
import com.zhejiangzhengyuan.municipal_common.utils.ConstantUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;
import java.util.Objects;

/**
 * @author lin_ju
 * @since on 2019/12/30
 */
@RestController
@RequestMapping("/floodedRoad/timeMonitoring")
@Api(tags = "道路积水实时监控")
public class FloodRoadTimeMonitoringController {

    @Autowired
    private DeviceService deviceService;
    @Autowired
    private DeviceMonitorValueService deviceMonitorValueService;
    @Autowired
    private ElectronicDataMapper electronicDataMapper;
    private String deviceType = ConstantUtil.DEVICE_TYPE.FLOODED_ROAD;
    private String moduleName = ConstantUtil.MODULE_NAME.FLOODED_ROAD;

    @ApiOperation(value = "查询设备跟设施", response = DeviceVo.class)
    @GetMapping("/getByPage")
    public ResponseVo getDeviceAndFacility() {
        List<DeviceVo> deviceVos = deviceService.getDeviceAndFacility(deviceType);
        return deviceVos == null ? ResponseVo.SERVER_ERROR : ResponseVo.successWithData(deviceVos);
    }

    @ApiOperation("设置道路积水设备阈值")
    @PutMapping("/settingThreshold")
    public ResponseVo settingThreshold(@RequestBody DeviceMonitorValueVo deviceMonitorValueVo) {
        List<DeviceMonitorValue> deviceMonitorValues =
                DeviceMonitorValueVo.formatPo(deviceMonitorValueVo, moduleName);
        QueryWrapper<DeviceMonitorValue> deviceMonitorValueQueryWrapper;
        for (DeviceMonitorValue deviceMonitorValue : deviceMonitorValues) {
            deviceMonitorValueQueryWrapper = new QueryWrapper<>();
            deviceMonitorValueQueryWrapper
                    .eq("module_type", moduleName)
                    .eq("grade", deviceMonitorValue.getGrade());
            DeviceMonitorValue value = deviceMonitorValueService.getOne(deviceMonitorValueQueryWrapper);
            if (Objects.nonNull(value)) {
                deviceMonitorValue.setId(value.getId());
            }
        }
        return deviceMonitorValueService.saveOrUpdateBatch(deviceMonitorValues)
                ? ResponseVo.successWithData(deviceMonitorValueVo)
                : ResponseVo.SERVER_ERROR;
    }

    @ApiOperation("获取道路积水设备阈值")
    @GetMapping("/getThreshold")
    public ResponseVo settingThreshold() {
        // 阈值只有一条
        QueryWrapper<DeviceMonitorValue> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("module_type", moduleName);
        List<DeviceMonitorValue> list = deviceMonitorValueService.list(queryWrapper);
        return Objects.isNull(list) || list.isEmpty()
                ? ResponseVo.successWithData(list)
                : ResponseVo.successWithData(DeviceMonitorValueVo.formatVo(list));
    }

    @ApiOperation("根据设备code获取过去24小时积水值")
    @GetMapping("/getWaterValueByDeviceCode")
    public ResponseVo getWaterValueByDeviceCode(String deviceCode) {
        return deviceService.getWaterValueByDeviceCode(deviceCode);
    }

    @ApiOperation("查询历史记录")
    @GetMapping("/selectElectronicData")
    public ResponseVo selectElectronicData(
            @ApiParam("页码") Long pageNo, @ApiParam("每页显示条数") Long pageSize) {
        // pageNo默认为1；size默认为14
        pageNo = pageNo == null || pageNo <= 0 ? 1 : pageNo;
        pageSize = pageSize == null || pageSize <= 0 ? 7 : pageSize;
        Page<ElectronicData> electronicDataPage = new Page<>(pageNo, pageSize);
        Page<ElectronicData> electronicData =
                electronicDataMapper.selectElectronicData(electronicDataPage);
        return electronicData == null
                ? ResponseVo.SERVER_ERROR
                : ResponseVo.successWithData(electronicData);
    }

    @ApiOperation("模糊查询")
    @GetMapping("/fuzzyElectronicData")
    public ResponseVo fuzzyElectronicData(String name, String startTime, String endTime,
                                          @ApiParam("页码") Long pageNo, @ApiParam("每页显示条数") Long pageSize) throws ParseException {
        // pageNo默认为1；size默认为14
        pageNo = pageNo == null || pageNo <= 0 ? 1 : pageNo;
        pageSize = pageSize == null || pageSize <= 0 ? 7 : pageSize;
        Page<ElectronicData> electronicDataPage = new Page<>(pageNo, pageSize);
        Page<ElectronicData> electronicData = electronicDataMapper.fuzzyElectronicData(electronicDataPage, name, startTime, endTime);
        return electronicData == null ?
                ResponseVo.SERVER_ERROR :
                ResponseVo.successWithData(electronicData);
    }
}
